// 管理员列表

import request from '@/utils/request'
import { Ilistparams, IlistResponse, IaddBodyadmin, IBodyupdateAminInfo } from './types/admin'
import { IFormData } from './types/form'

// 获取管理员列表
export const getAdminList = (params: Ilistparams) => {
    return request<{
        count: number
        list: IlistResponse[]
    }>({
        method: 'GET',
        url: '/setting/admin',
        params
    })
}

// 添加管理员
export const addAdmin = (data: IaddBodyadmin) => {
    return request({
        method: 'POST',
        url: '/setting/admin',
        data
    })
}

// 修改管理员
export const updateAdmin = (id: number, data: IaddBodyadmin) => {
    return request({
        method: 'PUT',
        url: `/setting/admin/${id}`,
        data
    })
}

// 删除管理员
export const deleteAdmin = (id: number) => {
    return request({
        method: 'DELETE',
        url: `setting/admin/${id}`
    })
}

// 修改管理员状态
export const updateAdminStatus = (id: number, status: number) => {
    return request({
        method: 'PUT',
        url: `setting/set_status/${id}/${status}`
    })
}

// 修改管理员信息
export const updateAdminInfo = (data: IBodyupdateAminInfo) => {
    return request({
        method: 'PUT',
        url: '/setting/update_admin',
        data
    })
}

// 管理员添加表单
export const getRoles = () => {
    return request<IFormData>({
        method: 'GET',
        url: '/setting/admin/create'
    }).then(data => {
        const roles = data.rules.find(item => item.field === 'roles')
        if (roles && roles.options) {
            return roles.options
        }
        return []
    })
}

// 获取修改管理员表单
export const getAdmin = (id: number) => {
    return request<IFormData>({
        method: 'GET',
        url: `/setting/admin/${id}/edit`
    }).then(data => {
        const obj: Record<string, any> = {}
        data.rules.forEach(item => {
            obj[item.field] = item.value
        })
        return obj as {
            account: string
            pwd: string
            conf_pwd: string
            real_name: string
            roles: string[]
            status: 0 | 1
        }
    })
}

// setting/info 获取当前登录管理员信息

export const getAdminInfo = () => {
    return request({
        method: 'GET',
        url: '/setting/info'
    })
}
